//jschip8.js


//>>16 == 0xF
//>>8 == 0x0F
//>>4 == 0x00F
//>>2 == 0x000F
//else F = opcode Position

var V = new Array(16);
var PC;
var memory = new Array(0xFFF);
var I;

var lol = document.getElementById("lol");
lol.getContext('2d');

start_vm = function()
{
	PC = 0x200;
	for (var x = 0; x <= 16; x++)
		V[x] = 0;
	for(;;) {
		EmulateCPU();
	}
}

EmulateCPU = function()
{
	var opcode = ((memory[PC] << 8) + memory[PC+1]);
	switch (opcode&0xF000)
	{
     case 0x1000:
        PC = (opcode&0x0FFF);
        break;
		 case 0x00E0:
			 lol.clear();
			 PC += 2;
			 break;
		 case 0x6000:
			 V[((opcode&0x0F000) >> 8)] = opcode&0x00FF;
			 PC += 2;
			 break;
		 case 0x5000:
			 if (V[((opcode&0x0F00) >> 8)] == V[((opcode&0x00F0) >> 4)])
				 PC += 4;
			 else
				 PC += 2;
			break;
     case 0x3000:
       if (V[((opcode&0x0F000) >> 8)] == opcode&0x00FF)
         PC += 4;
       else
         PC += 2;
       break;
    case 0x7000:
       V[((opcode&0x0F000) >> 8)] += (opcode&0x00FF);
       PC += 2;
       break;
	}
}
